package org.chnxi.leetcode.problems.ques7;

/**
 * 执行用时 :1 ms, 在所有 Java 提交中击败了100.00%的用户
 * 内存消耗 :36.8 MB, 在所有 Java 提交中击败了5.33%的用户
 */
public class Simple1 {

    public static void main(String[] args) {
        int num1 = 123;
        System.out.println(num1+",reverse="+reverse(num1));

        int num2 = -123;
        System.out.println(num2+",reverse="+reverse(num2));

        int num3 = Integer.MAX_VALUE-1;
        System.out.println(num3+",reverse="+reverse(num3));

        int num4 = Integer.MIN_VALUE+1;
        System.out.println(num4+",reverse="+reverse(num4));
    }

    public static int reverse(int x) {
        long result = 0;
        int tmp = x;

        while (tmp < -9 || tmp > 9){
            result = result * 10 + (tmp % 10);
            tmp = tmp / 10;
        }
        //处理个位数
        result = result*10 + tmp;
        //判断是否超出int范围
        if(result < Integer.MIN_VALUE || result > Integer.MAX_VALUE){
            return 0;
        }
        return (int)result;
    }
}
